<?php

namespace App\Http\Controllers\Admin;

use App\Models\Permissions;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Silber\Bouncer\Database\Ability;
use Silber\Bouncer\Database\Role;

class RolesController extends Controller
{
    public function __construct()
    {
        return view()->share('menu','admin');
    }

    /**
     * 角色列表
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $like_title = '';

        $query = Role::select('id','name','title','created_at','updated_at')
            ->where('id','!=',1)
            ->orderBy('id', 'DESC');
        if (empty($request->post('like_title'))) {
            $list = $query ->paginate(15);
        } else {
            $like_title = $request->post('like_title');
            $list = $query
                ->where('title', 'like', '%' . $like_title . '%')
                ->paginate(15);
            $url_data = compact('like_title');
        }
        $page_num = $list->firstItem();

        return view('admin/role/index', compact('list', 'page_num', 'like_title', 'url_data'));
    }

    /**
     * 添加角色
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {

        $list = Ability::get()->pluck('title', 'name');

        return view('admin/role/create', compact('list'));
    }

    /**
     * 保存角色
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {

        $data = $request->all();
        if (empty($data['title'])){
            return back()->with(['msg'=>'角色名称必须！','status'=>0]);
        }
        if (empty($data['name'])){
            return back()->with(['msg'=>'资源必须！','status'=>0]);
        }
        $role_data = ['name'=>$data['title']];
        if(Role::where($role_data)->count() > 0){
            return back()->with(['msg'=>'角色名已存在！','status'=>0]);
        }
        $role = Role::create($role_data);
        $role->allow($request->input('name'));

        return back()->with(['msg'=>'添加成功','status'=>1,'msg_url'=>'/admin/role_index']);

    }

    /**
     * 编辑角色
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $list = Ability::select('id','title','name')->get();
        //->pluck('title', 'name');

        $data = Role::findOrFail($id);
        //查询该角色已分配的资源
        $abilities = Permissions::where(['entity_id'=>$id])->pluck('ability_id')->toArray();

        return view('admin/role/edit', compact('list', 'data','abilities'));
    }

    /**
     * 更新角色
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, $id)
    {
        $role = Role::findOrFail($id);
        $data = $request->all();
        $role->update(['name'=>$data['title'],'title'=>$data['title']]);
        foreach ($role->getAbilities() as $ability) {
            $role->disallow($ability->name);
        }
        $role->allow($request->input('name'));

        return back()->with(['msg'=>'修改成功','status'=>1,'msg_url'=>'/admin/role_index']);

    }

    /**
     * 删除角色
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Request $request)
    {
        $id = $request->get('id');
        $role = Role::findOrFail($id);
        $role->delete();

        return ['status'=>1,'msg'=>'删除成功'];
    }
}
